/**
 * @date 2022/4/6
 * @description 处理后端返回的菜单表生成动态路由表
 * @author 
 */

import Layout from '@/layouts/index.vue'
import { dfs } from '@/utils/common.js'

export function filterAsyncRoutes(routerSheet) {
    const newArry = dfs(routerSheet)
    let res = []
    const level1 =
        import.meta.glob('../views/*/*.vue')
    const level2 =
        import.meta.glob('../views/*/*/*.vue')
    const level3 =
        import.meta.glob('../views/*/*/*/*.vue')
    let modules = {
        ...level1,
        ...level2,
        ...level3,
    }
    const addchild = (child, fatherIndex) => {
        let childArr = []
        if (child.length > 0) {
            childArr = child.map((cc, childIndex) => {
                return {
                    path: cc.path,
                    name: cc.name,
                    component: modules[`../views${cc.href}.vue`],
                    // redirect: cc.childresource && cc.childresource.length > 0 ? cc.childresource[0].path : '',
                    children: cc.childresource && cc.childresource.length > 0 ? addchild(cc.childresource) : [],
                    meta: {
                        title: cc.menucn,
                        icon: cc.icon,
                        affix: childIndex === 0 && fatherIndex === 0 ? true : false,
                        noKeepAlive: childIndex === 0 && fatherIndex === 0 ? true : false,
                        level: cc.level,
                    },
                }
            })
        }
        return childArr
    }
    if (newArry.length > 0) {
        newArry.forEach((item, i) => {
            res.push({
                name: item.name,
                path: i === 0 ? '/' : item.path,
                component: Layout,
                redirect: item.childresource && item.childresource.length > 0 ? item.childresource[0].path : '',
                children: item.childresource && item.childresource.length > 0 ? addchild(item.childresource, i) : [],
                meta: {
                    title: item.menucn,
                    icon: item.icon,
                    click: item.valid,
                    level: item.level,
                },
            })
        })
    }
    return res
}